package com.light.remote.client.helpingclasses;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MH
 */
public class MessagingServices {
    
    public static final int FLAG_TEXT = 10;
    public static final int FLAG_IMAGE = 11;
    public static final int FLAG_GET_IMAGE = 15;
    public static final int FLAG_TEXT_FILE = 12;
    public static final int FLAG_MOUSE=13;
    public static final int FLAG_KEY=14;
    public static final int FLAG_SETTING=16;
    
    private DataOutputStream out;
    
    public MessagingServices(DataOutputStream out){
        this.out=out;
    }
    
    public void getNextImage(int w,int h){
        try {
            out.writeInt(FLAG_GET_IMAGE);
            out.writeInt(w);
            out.writeInt(h);
            out.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void sendText(final String msg) {
        new Thread() {
            public void run() {
                try {
                    out.writeInt(FLAG_TEXT);
                    out.writeUTF(msg);
                    out.flush();
                } catch (Exception ex){
                }
            }
        }.start();
    }
    
    public void sendKey(final int msg) {
        new Thread(){
            public void run() {
                try {
                    out.writeInt(FLAG_KEY);
                    out.writeInt(msg);
                    out.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    
    public void sendMouseMotion(final int msg) {
        new Thread(){
            public void run() {
                try {
                    out.writeInt(FLAG_MOUSE);
                    out.writeInt(msg);
                    out.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    
    public void sendImage(final String resourceImagePath) {
        new Thread() {
            public void run() {
                try {
                    byte[] bytes = getImageBytes(resourceImagePath);
                    out.writeInt(FLAG_IMAGE);
                    out.writeInt(bytes.length);
                    out.write(bytes);
                    out.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    
    public void sendImage(final byte[] imageBytes) {
        new Thread() {
            public void run() {
                try {
                    out.writeInt(FLAG_IMAGE);
                    out.writeInt(imageBytes.length);
                    out.write(imageBytes);
                    out.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    
    public byte[] getImageBytes(String resource) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream bais;
        try {
            bais = (InputStream) getClass().getResourceAsStream(resource);
            int c;
            while ((c = bais.read()) != -1) {
                baos.write(c);
            }
            bais.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
}
